package cn.powerchaintech.common.cimeparser;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;

public class CimEReader {

    public CimEDocument read(InputStream inputStream, Charset cs) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, cs));
        String line;
        CimEBlock cimEBlock = null;
        CimEDocument doc = new CimEDocument();
        while ((line = reader.readLine()) != null) {
            if (line.trim().isEmpty()) {
                continue;
            }
            if (CimEDescription.IsDescription(line)) {
                CimEDescription cimEDescription = new CimEDescription(line);
                doc.setDescription(cimEDescription);
                continue;
            }
            if (CimEBlock.IsStartBlock(line)) {
                cimEBlock = new CimEBlock(line);
            }
            if (cimEBlock != null) {
                if (cimEBlock.isEndBlock(line)) {
                    doc.append(cimEBlock);
                    cimEBlock = null;
                } else {
                    cimEBlock.append(line);
                }
            }
        }
        return doc;
    }
}
